package com.primb.bench.project.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.mikephil.charting.formatter.IFillFormatter;
import com.primb.baselibs.util.FormatUtils;
import com.primb.baselibs.util.KeyboardWatcher;
import com.primb.baselibs.view.AutoToolbar;
import com.primb.bench.R;
import com.primb.bench.base.SwipeBaseActivity;
import com.primb.bench.common.constant.AppConstant;
import com.primb.bench.common.dialog.SelectListDialog;
import com.primb.bench.project.home.contract.AddEnsureInfoContract;
import com.primb.bench.project.home.entity.EnsureObjectEntity;
import com.primb.bench.project.home.entity.KeyEntity;
import com.primb.bench.project.home.presenter.AddEnsureInfoPresenterImpl;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.primb.bench.common.constant.AppConstant.EDIT_ENSURE_OBJECT_RESULT;

/**
 * Created by Chen on 2017/5/16.
 * 功能描述：
 */

public class AddEnsureInfoActivity extends SwipeBaseActivity implements AddEnsureInfoContract.View {

    @BindView(R.id.title_bar)
    TextView titleBar;
    @BindView(R.id.iv_top)
    ImageView ivTop;
    @BindView(R.id.tool_bar)
    AutoToolbar toolBar;
    @BindView(R.id.tv_ensure_object_type)
    TextView tvEnsureObjectType;
    @BindView(R.id.et_ensure_object_money)
    EditText etEnsureObjectMoney;
    @BindView(R.id.btn_ensure_object_save)
    Button btnEnsureObjectSave;
    @BindView(R.id.tv_ensure_type)
    TextView tvEnsureType;
    private EnsureObjectEntity info;
    private AddEnsureInfoPresenterImpl mPresenter;
    private boolean isNew = true;
    private String priceId;
    private String type;
    private KeyboardWatcher keyboardWatcher;

    @Override
    protected void onBaseIntent() {
        super.onBaseIntent();
        Intent intent = getIntent();
        info = intent.getParcelableExtra("info");
        type = intent.getStringExtra("type");
        priceId = intent.getStringExtra("priceId");
        if (info != null) {
            isNew = false;
        } else {
            info = new EnsureObjectEntity();
        }
    }

    @Override
    protected int onBaseLayoutId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_ensure_edit;
    }

    @Override
    protected void onBaseBindView() {
        titleBar.setText("抵质押品信息");
        toolBar.setNavigationOnClickListener(v -> AddEnsureInfoActivity.this.finish());
        ivTop.setImageResource(R.mipmap.reset_icon);
        ivTop.setVisibility(View.VISIBLE);
//        if (type.equals("保证")) {
//            tvEnsureType.setText(type);
//        }
    }

    @Override
    protected void onLoadData() {
        mPresenter = new AddEnsureInfoPresenterImpl(this, this, priceId, info);
        mPresenter.start();
        if (!isNew) {
            setEntityToView(info);
        }
//        keyboardWatcher = new KeyboardWatcher(this);
//        keyboardWatcher.setListener(new KeyboardWatcher.OnKeyboardToggleListener() {
//            @Override
//            public void onKeyboardShown(int keyboardSize) {
//                String money = etEnsureObjectMoney.getText().toString();
//                if (null != money && !money.equals("")) {
//                    try {
//                        etEnsureObjectMoney.setText(money.replace(",", ""));
//                    } catch (Exception e) {
//
//                    }
//                }
//            }
//
//            @Override
//            public void onKeyboardClosed() {
//                String money = etEnsureObjectMoney.getText().toString();
//                if (null != money && !money.equals("")) {
//                    try {
//                        Double integer = Double.valueOf(money);
//                        String format2Double = FormatUtils.format2Double(integer);
//                        etEnsureObjectMoney.setText(format2Double);
//                    } catch (Exception e) {
//                        showErrorMsg("担保金额格式有误");
//                    }
//                }
//            }
//        });
    }

    @Override
    public void showLoading() {
        showLoadingDialog("加载中");
    }

    @Override
    public void hideLoading() {
        hideLoadingDialog();
    }

    @Override
    public void showErrorMsg(String msg) {
        showToast(msg);
    }

    @Override
    public void setEntityToView(EnsureObjectEntity entity) {
        tvEnsureType.setText(entity.getGuatantDesc());
        tvEnsureObjectType.setText(entity.getPawnDesc());
        etEnsureObjectMoney.setText(entity.getPawnAmt());
    }

    @Override
    public void resetInfo() {
        if (!isNew) {
            setEntityToView(info);
        } else {
            info.setPawnAmt("");
            info.setPawnDesc("");
            info.setPawnCode("");
            info.setEnsureObjectEmpty(true);
            tvEnsureObjectType.setText("抵质押品类型");
//            if (type.equals("保证"))
//                tvEnsureType.setText(type);
//            else {
            info.setGuatantDesc("");
            info.setGuatantCode("");
            tvEnsureType.setText("担保方式");
//            }
            etEnsureObjectMoney.setText("");
        }
    }

    @Override
    public SelectListDialog showListDialog(int requestCode) {
        SelectListDialog listDialog = new SelectListDialog();
        listDialog.setListener((adapter, v, position) -> {
            setDataToView(requestCode, listDialog.getDataList().get(position));
            mPresenter.handleInfoForList(requestCode, listDialog.getDataList().get(position));
            listDialog.dismiss();
        });
        listDialog.show(getSupportFragmentManager());
        return listDialog;
    }

    private void setDataToView(int code, KeyEntity entity) {
        if (code == AppConstant.ADD_MAIN_TYPE) {
            tvEnsureType.setText(entity.getKey());
            tvEnsureObjectType.setText("抵质押品类型");
        } else {
            tvEnsureObjectType.setText(entity.getKey());
        }
    }

    @Override
    public boolean getIsNew() {
        return isNew;
    }

    @Override
    public void saveResultForBack() {
        if (isNew)
            setResult(AppConstant.ADD_ENSURE_OBJECT_RESULT);
        else
            setResult(AppConstant.EDIT_ENSURE_OBJECT_RESULT);
        this.finish();
    }

    @Override
    public String getEnsureMoney() {
        return etEnsureObjectMoney.getText().toString();
    }

    @OnClick({R.id.iv_top, R.id.tv_ensure_object_type, R.id.tv_ensure_type, R.id.btn_ensure_object_save})
    public void onViewClicked(View view) {
        mPresenter.onViewClick(view.getId());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.onRecycle();
    }
}
